package util;

////import java.sql.Connection;
//import java.sql.Connection;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DbUtil {
    public static final String URL = "jdbc:h2:~/travel";
    public static final String USER = "root";
    public static final String PASSWORD = "root";

    private static Connection conn = null;

    static {
        try {
            //加载驱动
            Class.forName("org.h2.Driver");
            //获得数据库连接
            conn = DriverManager.getConnection(URL, USER, PASSWORD);
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
    }

    public static Connection getConnection(){
        return conn;
    }

    public static int isNum(String str){        //0字符串，1小数，2整数
        char[] ch = str.toCharArray();
        int len = str.length();
        if (str.length() == 0)return 0;
        int cnt=1,i;
        System.out.println("len = "+str.length());
        for (i=0;i<str.length();i++){
            if (ch[i]>='0'&&ch[i]<='9'){
                continue;
            }else if (cnt!=0 && i!=str.length()-1 && ch[i] == '.'){
                cnt--;
            }else {
                break;
            }
        }
        if (i == str.length() && cnt==0) return 1;
        else if (cnt==1 && i== str.length()) return 2;
        else return 0;
    }

}
